﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using Sunny.UI;

namespace IndustryFieldCommunication
{
    public partial class FConnectManage : UIPage
    {
        public FConnectManage(UIForm form, UINavMenu navMenu)
        {
            InitializeComponent();
            //创建交互页面
            MainForm = form;
            Aside = navMenu;
            OnAcceptOne = (client) =>
            {
                var idx = client.RemoteEndPoint.ToString().LastIndexOf('.');
                var page = new FEPPallet(client);
                page.Text = "ROB" + client.RemoteEndPoint.ToString().Remove(0, idx);
                var id = Guid.NewGuid();
                var node = Aside.CreateNode(MainForm.AddPage(page, id));
                ConnectList.Items.Add(
                    new EndPointRel(client, page, id, node));
            };
            Listener = new(OnAcceptOne);
        }

        private void IPComboBox_DropDown(object sender, EventArgs e)
        {
            IPComboBox.Items.Clear();
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress address in ipHostInfo.AddressList)
            {
                if (address.ToString().StartsWith("192.168."))
                {
                    IPComboBox.Items.Add(address);
                }
            }
        }

        private void BTListen_Click(object sender, EventArgs ea)
        {
            if (Listener.IsStoped())
            {
                var addr = (IPAddress)IPComboBox.SelectedItem;
                int port = PortTextBox.Text.ToInt();
                if (addr != null && port > 1024)
                {
                    Listener.StartListen(addr, port);
                    BTListen.Text = "停止监听";
                }
            }
            else
            {
                Listener.StopListen();
                BTListen.Text = "开始监听";
            }
        }

        private void ButtonDelete_Click(object sender, EventArgs ea)
        {
            var item = (EndPointRel)ConnectList.SelectedItem;
            try
            {
                if (item != null)
                {
                    item.Page.Dispose();
                    MainForm.RemovePage(item.PageID);
                    ConnectList.Items.Remove(item);
                    Aside.Nodes.Remove(item.Node);                 
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "ButtonDelete_Click", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        Listener Listener;
        Action<Socket> OnAcceptOne = null;
        UIForm MainForm;
        UINavMenu Aside;

        private class EndPointRel
        {
            public Socket EndPoint;
            public UIPage Page;
            public Guid PageID;
            public TreeNode Node;

            public EndPointRel(Socket EP, UIPage P, Guid ID, TreeNode N)
            {
                EndPoint = EP;
                Page = P;
                PageID = ID;
                Node = N;
            }
            public override string ToString()
            {
                try
                {
                    return EndPoint.RemoteEndPoint.ToString();
                }
                catch (ObjectDisposedException)
                {
                    return Page.Text + "已断开";
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString(), "在ToString", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return "错误";
                }
            }

        }
    }
}
